package pl.edu.mimuw.irs.core.util.graph;

import java.util.Collection;

import pl.edu.mimuw.irs.core.util.graph.exception.GraphException;
import pl.edu.mimuw.irs.core.util.graph.exception.GraphException.Cause;

public class Edge {

	// *** Attributes
	private Object node1;
	private Object node2;
	public Object getNode1() {
		return node1;
	}
	public Object getNode2() {
		return node2;
	}
	
	// *** Constructors
	
	public Edge(Object node1, Object node2) {
		this.node1 = node1;
		this.node2 = node2;
	}
	
	// *** Methods

	public boolean links(Object node) {
		if (node1.equals(node) || node2.equals(node)) return true;
		else return false;
	}
	
	public boolean links(Object node1, Object node2) {
		if (node1.equals(node1) && node2.equals(node2) ||
				node1.equals(node2) && node2.equals(node1)) return true;
		else return false;
	}
	
	public Object getContainedNode(Collection<? extends Object> set) {
		if (set.contains(node1)) {
			if (set.contains(node2)) {
				throw new GraphException(Cause.TOO_MANY_VERTEX_OCCURENCES);
			} else {
				return node1;
			}
		} else {
			if (set.contains(node2)) {
				return node2;
			} else {
				throw new GraphException(Cause.NO_VERTEX_OCCURENCES);
			}
		}
	}
	
	@Override
	public String toString() {
		return "Edge(" + getNode1() + "--" + getNode2() + ")";
	}
}
